/** AddAppointmentPanel */
package client.ui.panel;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.ListCellRenderer;

import client.service.AppointmentService;
import client.service.DoctorService;
import client.service.PatientService;
import domain.appointment.Appointment;
import domain.doctor.Doctor;
import domain.patient.Patient;
import domain.records.TreatmentRecord;
import domain.user.SystemUser;

/**
 * Panel for adding new appointments
 * @author jessienewman
 *
 * @version $Revision: 1.0 $
 */
public class AddAppointmentPanel extends DefaultPanel {
	/**
	 * Field center.
	 */
	private final JPanel center;

	/**
	 * Field user.
	 */
	private SystemUser user;
	
	/**
	 * Field reason.
	 */
	private JTextField reason; // $codepro.audit.disable unusedField, com.instantiations.assist.eclipse.analysis.unassignedField
	
	/**
	 * Field ampm.
	 */
	/**
	 * Field minutes.
	 */
	/**
	 * Field hours.
	 */
	/**
	 * Field date.
	 */
	/**
	 * Field doctors.
	 */
	/**
	 * Field patients.
	 */
	private JComboBox patients, doctors, date, hours, minutes, ampm;
	
	/**
	 * Field reset.
	 */
	/**
	 * Field save.
	 */
	private JButton save;
	
	/**
	 * Field error.
	 */
	private JLabel error; // $codepro.audit.disable variableShouldBeFinal
	
	/**
	 * Field c.
	 */
	private GridBagConstraints c;
	
	/**
	 * Field numDates.
	 */
	private static final Integer NUMDATES = 14;

	/** creates an addAppointmentPanel */
	public AddAppointmentPanel() {
		center = new JPanel();
		add(center, BorderLayout.CENTER);
		c = new GridBagConstraints();
		center.setLayout(new GridBagLayout());
		center.setBackground(Color.WHITE);
		error = new JLabel(" ");
	}

	/**
	 * adds a user to the panel so panel can be fileld with information
	 * @param user user who's information should be used. 
	 * Will be either the patient or the
	 * dcotor of the appointment
	 */
	public void addUser(SystemUser user) {
		this.user = user;
		c.gridx = 0;
		c.gridy = 0;
		c.fill = GridBagConstraints.HORIZONTAL;

		c.gridwidth = 4; // $codepro.audit.disable numericLiterals
		center.add(error);
		c.gridy++;

		c.gridwidth = 1;
		final Date[] dateArr = new Date[NUMDATES];
		for (int i = 0; i < NUMDATES; i++) {
			Date temp = new Date();
			temp.setDate(temp.getDate() +  i);
			dateArr[i] = temp;
		}
		
		date = new JComboBox(dateArr);
		date.setRenderer(new DateRenderer());
		final Integer[] hoursArr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; // $codepro.audit.disable numericLiterals
		final Integer[] minutesArr = {00, 15, 30, 45}; // $codepro.audit.disable numericLiterals
		final String[] ampmArr = {"AM", "PM"};
		hours = new JComboBox(hoursArr);
		minutes = new JComboBox(minutesArr);
		ampm = new JComboBox(ampmArr);

		center.add(new JLabel("Date: "), c);
		c.gridx++;
		c.gridwidth = 3; // $codepro.audit.disable numericLiterals
		center.add(date, c);
		c.gridy++;
		c.gridx = 0;

		c.gridwidth = 1;
		center.add(new JLabel("Time: "), c);
		c.gridx++;
		center.add(hours, c);
		c.gridx++;
		center.add(minutes, c);
		center.add(new JLabel(":"));
		c.gridx++;
		center.add(ampm, c);
		c.gridy++;
		c.gridx = 0;

		center.add(new JLabel("Doctor: "), c);
		c.gridx++;
		doctors = new JComboBox(DoctorService.getAllDoctors().toArray());
		doctors.setRenderer(new UserRenderer());
		c.gridwidth = 3; // $codepro.audit.disable numericLiterals
		center.add(doctors, c);

		c.gridy++;
		c.gridx = 0;
		c.gridwidth = 1;
		center.add(new JLabel("Patient: "), c);
		c.gridx++;
		patients = new JComboBox(PatientService.getAllPatients().toArray());
		patients.setRenderer(new UserRenderer());
		c.gridwidth = 3; // $codepro.audit.disable numericLiterals
		center.add(patients, c);
		
		if (user instanceof Doctor) {
			doctors.setSelectedItem((Doctor) user);
		} else if (user instanceof Patient) {
			patients.setSelectedItem((Patient) user);
			patients.setEditable(false);
		}

		c.gridwidth = 1;
		c.gridy++;
		c.gridx = 0;
		save = new JButton("Save");
		save.addActionListener(new ButtonListener());
		center.add(save, c);

	}

	/** saves appointment to DATABASE on click of 'save' 
	 * @author jessienewman
	 */
	private class ButtonListener implements ActionListener { // $codepro.audit.disable com.instantiations.assist.eclipse.analysis.audit.rule.effectivejava.alwaysOverridetoString.alwaysOverrideToString
		/**
		 * Method actionPerformed.
		 * @param event ActionEvent
		
		 * @see java.awt.event.ActionListener#actionPerformed(ActionEvent) */
		public void actionPerformed(ActionEvent event) {
			if (event.getSource().equals(save)) {
				//try {
					final Date day = (Date) date.getSelectedItem();
					day.setHours((Integer) hours.getSelectedItem());
					day.setMinutes((Integer) minutes.getSelectedItem());
					final Doctor doctor = ((Doctor) doctors.getSelectedItem());
					//final Patient p = ((Patient) patients.getSelectedItem());
					final Appointment appt = new Appointment(doctor, (Patient) user, day, 
							new TreatmentRecord(doctor, (Patient) user));
					AppointmentService.addAppointment(appt);
					error.setText("Appointment Saved");
			} 
		}
	}

	/** shows teh dates in the format MMMMM dd, yyyy  
	 * @author jessienewman
	 */
	private static class DateRenderer extends JLabel implements ListCellRenderer {
		/**
		 * Method getListCellRendererComponent.
		 * @param list JList
		 * @param value Object
		 * @param index int
		 * @param isSelected boolean
		 * @param cellHasFocus boolean
		 * @return Component
		 * @see javax.swing.ListCellRenderer#getListCellRendererComponent(
		 * JList, Object, int, boolean, boolean)
		 */
		@Override
		public Component getListCellRendererComponent(JList list, Object value,
				int index, boolean isSelected, boolean cellHasFocus) {
			final SimpleDateFormat sdf = new SimpleDateFormat("MMMMM dd, yyyy");
			setText(sdf.format((Date) value));
			return this;
		}
	}
	
	/** shows teh user's full names * 
	 * @author jessienewman
	 */
	private static class UserRenderer extends JLabel implements ListCellRenderer {
		/**
		 * Method getListCellRendererComponent.
		 * @param list JList
		 * @param value Object
		 * @param index int
		 * @param isSelected boolean
		 * @param cellHasFocus boolean
		 * @return Component
		 * @see javax.swing.ListCellRenderer#getListCellRendererComponent(
		 * JList, Object, int, boolean, boolean)
		 */
		@Override
		public Component getListCellRendererComponent(JList list, Object value,
				int index, boolean isSelected, boolean cellHasFocus) {
			setText(((SystemUser) value).getFullName());
			return this;
		}
	}
	
	/**
	 * returns the super's toString
	
	 * @return String super's toString */
	public String toString() {
		return super.toString();
	}
}
